/**
 * Created by huangzx on 2015/12/7.
 */
define(['angular',
        'system2/user/UserController',
        'system2/account/AccountController',
        'system2/logger/LoggerController',
        'system2/project/ProjectManagerController',
        'system2/package/PackageManagerController',
        'system2/datadict/DataManagerController',
        'system2/holidays/HolidaysController',
        'system2/monitor/ReportController',
        'angular-route',
        'framework/msg/GillionMsgModule',
        'framework/dataGrid/DataGridModule',
        'framework/pagination/GillionPaginationModule',
        'framework/dropdown/GillionDropdownModule',
        'framework/clickbox/GillionClickBoxModule',
        'framework/date/DateModule',
        'framework/associate/GillionAssociateModule',
        'components/DateRangerModule',
        'system2/RulePermissionModule',
        'bootstrap'],
    function (angular, UserController, AccountController, LoggerController, ProjectManagerController, PackageManagerController,DataManagerController,HolidaysController, ReportController) {
        var app = angular.module('SystemModule', ['ngRoute', 'GillionClickBoxModule', 'GillionMsgModule', 'DataGridModule', 'GillionPaginationModule', 'GillionDropdownModule', 'DateModule', 'GillionAssociateModule','DateRangerModule', 'RulePermissionModule']);
        app.controller('HeaderController', ['$scope', '$rootScope', '$location', '$http', 'GillionMsg', 'Permissions', function ($scope, $rootScope, $location, $http, GillionMsg, Permissions) {
            $scope.account = $rootScope.$sessionAttrs.loginUser;
            $scope.currUser = $scope.account.username;
            $scope.monitorShow=false;
            $scope.getParams = function () {
                window.location.href = "/html2/system#/monitor";
            };
            /**
             * 注销当前用户,跳转回登录页面
             */
            $scope.logout = function () {
                $http.get("/grule-manager/user/logout").success(function (result) {
                    if (result.success) {
                        window.location.href = "/html2/login/login"
                    }
                });
            };
            $scope.log = function () {
                var url = window.location.href;
                var arr = new Array();
                var params;
                arr = url.split("/");
                if (6 < arr.length && arr.length < 8) {
                    params = "?" + arr[5] + "=" + arr[6];
                    window.location.href = "/html2/system#/logManager" + params;
                }
                else if (arr.length > 8) {
                    params = "?" + arr[5] + "=" + arr[6] + "&" + arr[7] + "=" + arr[8];
                    window.location.href = "/html2/system#/logManager" + params;
                }
                else {

                    window.location.href = "/html2/system#/logManager";
                }

            };
            $scope.monitor = function () {
                var url = window.location.href;
                var arr = new Array();
                var params;
                arr = url.split("/");
                if (6 < arr.length && arr.length < 8) {
                    params = "?" + arr[5] + "=" + arr[6];
                    window.location.href = "/html2/system#/monitor" + params;
                }
                else if (arr.length > 8) {
                    params = "?" + arr[5] + "=" + arr[6] + "&" + arr[7] + "=" + arr[8];
                    window.location.href = "/html2/system#/monitor" + params;
                }
                else {
                    window.location.href = "/html2/system#/monitor";
                }

            };

        }]);
        app.controller('MenuController', ['$scope', '$location', '$http', '$rootScope', function ($scope, $location, $http, $rootScope,Permissions) {
            $scope.admin = false;
            $scope.projectAdmin = false;

            //检查是否是超级管理员权限,若是,则直接授予所有权限,否则判定是否有项目管理员的权限
            var roles = $rootScope.$sessionAttrs.roles;
            $scope.admin = _.any(roles,function(role){
                return role.roleId == "1";
            });
            if ($scope.admin){
                $scope.projectAdmin = true;
            }else{
                $scope.projectAdmin = _.any($rootScope.$sessionAttrs.projectPermission,function(permission){
                    return _.any(permission.roles,function(role){
                        return role == 0;
                    });
                });
            }
        }]);


        app.controller('UserController', UserController);
        app.controller('AccountController', AccountController);
        app.controller('LoggerController', LoggerController);
        app.controller('ReportController', ReportController);
        app.controller('ProjectManagerController', ProjectManagerController);
        app.controller("PackageManagerController", PackageManagerController);
        app.controller("DataManagerController", DataManagerController);
        app.controller("HolidaysController", HolidaysController);

        app.config(['$locationProvider', '$routeProvider', function ($locationProvider, $routeProvider) {
            $routeProvider
                .when('/userManager', {
                    controller: 'UserController',
                    templateUrl: '/html2/system/user/userManagement'
                })
                .when('/accountManager', {
                    controller: 'AccountController',
                    templateUrl: '/html2/system/account/account'
                })
                .when('/logManager', {
                    controller: 'LoggerController',
                    templateUrl: '/html2/system/logger/logger'
                })
                .when("/project-manager", {
                    controller: "ProjectManagerController",
                    templateUrl: "/html2/system/project/project-manager"
                })
                .when("/package-manager", {
                    controller: "PackageManagerController",
                    templateUrl: "/html2/system/package/package-manager"
                })
                .when('/monitor', {
                    controller: 'ReportController',
                    templateUrl: '/html2/monitor/report'
                })
                .when('/data', {
                    controller: 'DataManagerController',
                    templateUrl: '/html2/system/dataDict/dataManager'
                })
                .when('/holidays', {
                    controller: 'HolidaysController',
                    templateUrl: '/html2/system/holidays/holidaysManager'
                })
                .otherwise({
                    redirectTo: '/accountManager'
                });
        }]);
        app.filter('typeFilter', function () {
            return function (input) {
                var valueTypeDict = ['节假日', '工作日'];
                return valueTypeDict[input] || '';
            };
        });
        return app;
    }
);
